package com.cdp.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysStone;
import com.cdp.system.dto.StoneDTO;
import com.cdp.system.mapper.SysStoneMapper;
import com.cdp.system.service.ISysStoneService;
import com.cdp.system.vo.IdsVO;
import com.cdp.system.vo.SysStoneVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SysStoneServiceImpl extends ServiceImpl<SysStoneMapper, SysStone>implements ISysStoneService {


    @Autowired
    private SysStoneMapper sysStoneMapper;
    /**
     * 石头信息
     * @return
     */
    @Override
    public List<SysStone> listStones() {
        return sysStoneMapper.listStones();
    }

    /**
     * 查询石头表字段的最大值最小值
     * @return
     */
    @Override
    public Map<String, Object> getStoneMinAndMax() {

        return  sysStoneMapper.getStoneMinAndMax();
    }

    @Override
    public AjaxResult listMistedStones(StoneDTO stoneDTO) {

     List<SysStoneVO> sysStoneVOS=    sysStoneMapper.listMistedStones(stoneDTO);

        return AjaxResult.success(sysStoneVOS);
    }

    /**
     * 石id模糊查询
     * @param id
     * @return
     */
    @Override
    public List<IdsVO> querySandIdsById(Integer id) {
        LambdaQueryWrapper<SysStone> wrapper = new LambdaQueryWrapper<>();


        // 当 id 不为空时，添加模糊查询条件
        if (id != null) {
            wrapper.like(SysStone::getStoneId, id);
        }

        List<SysStone> sysSands = sysStoneMapper.selectList(wrapper);

        IdsVO idsVO = new IdsVO();
        List<Integer> sandIds = sysSands.stream()
                .map(SysStone::getStoneId)
                .collect(Collectors.toList());
        idsVO.setIds(sandIds);
        return Collections.singletonList(idsVO);
    }

    @Override
    public List<SysStone> listStoneByIds(List<Integer> stoneIds) {
        if (stoneIds == null || stoneIds.isEmpty()) {
            return Collections.emptyList();
        }

        return sysStoneMapper.listStoneByIds(stoneIds);
    }

}
